﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using DataAccess;
using BusinessLogic;
using System.Data;

namespace ATM_System_Mock1_G2
{
    public partial class WithdrawScreen02 : System.Web.UI.Page
    {
        MoneyEntites moneyE = new MoneyEntites();
        MoneyDA moneyDA = new MoneyDA();
        MoneyBL moneyBL = new MoneyBL();
        AccountEntites accountE = new AccountEntites();
        AccountDA accountDA = new AccountDA();
        AccountBL accountBL = new AccountBL();
        LogEntites logE = new LogEntites();
        LogDA logDA = new LogDA();
        LogBL logBL = new LogBL();
        StockBL stock_BL = new StockBL();
        StockEntites stock_E = new StockEntites();
        CardEntites cardE = new CardEntites();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (txt_Nhap.Text != "")
            {
                Session["MoneyValue"] = int.Parse(txt_Nhap.Text);
                Session["Balance"] = int.Parse(txt_Nhap.Text);
            }
        }
        
        public void WriteLog()
        {
            logE.LogTypeID = 2;
            logE.ATMID = 2;
            logE.CardNo = Session["CardNo"].ToString();
            logE.LogDate = DateTime.Now;
            logE.Amount = int.Parse(txt_Nhap.Text);
            logE.Details = "Withdraw";
            logBL.writeLog(logE);
        }
        protected void btn_12_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("WithdrawScreen01.aspx");
        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
        {
           
            RutTien();
        }
        public void RutTien()
        {
            if (String.IsNullOrWhiteSpace(txt_Nhap.Text))
            {
                lb_ThongBao.Text = "* Amount is not null";
            }
            else {
                accountE.Balance = int.Parse(txt_Nhap.Text);
                decimal TienRut = decimal.Parse(txt_Nhap.Text);
              
                accountE.Balance = int.Parse(txt_Nhap.Text);
                cardE.CardNo = HttpContext.Current.Session["CardNo"].ToString();
                Decimal balance = accountBL.getBalance(cardE);
                Decimal moneyvalue = moneyBL.getMoneyValue();
                if ((balance > TienRut) && (TienRut % 50000 == 0) && (balance - TienRut > 50000) && (TienRut < 20000000))
                {
                    DataTable dt = new DataTable();
                    dt = stock_BL.GetQuantity(stock_E);
                    int T500k = int.Parse(dt.Rows[0]["Quantity"].ToString());
                    int T200k = int.Parse(dt.Rows[1]["Quantity"].ToString());
                    int T100k = int.Parse(dt.Rows[2]["Quantity"].ToString());
                    int T50k = int.Parse(dt.Rows[3]["Quantity"].ToString());
                    int T20k = int.Parse(dt.Rows[4]["Quantity"].ToString());
                    int T10k = int.Parse(dt.Rows[5]["Quantity"].ToString());

                    decimal TongTien = T500k * 500000 + T200k * 200000 + T100k * 100000 + T50k * 50000 + T20k * 20000 + T10k * 10000;
                    if (TienRut > TongTien)
                    {
                        lb_ThongBao.Text = "Ko đủ tiền";
                    }
                    else if (TienRut == TongTien)
                    {
                        stock_E.Quantity = T500k;
                        stock_E.MoneyID = 1;
                        stock_BL.UpdateQuantityATM(stock_E);

                        stock_E.Quantity = T200k;
                        stock_E.MoneyID = 2;
                        stock_BL.UpdateQuantityATM(stock_E);

                        stock_E.Quantity = T100k;
                        stock_E.MoneyID = 3;
                        stock_BL.UpdateQuantityATM(stock_E);

                        stock_E.Quantity = T50k;
                        stock_E.MoneyID = 4;
                        stock_BL.UpdateQuantityATM(stock_E);

                        stock_E.Quantity = T20k;
                        stock_E.MoneyID = 6;
                        stock_BL.UpdateQuantityATM(stock_E);

                        stock_E.Quantity = T10k;
                        stock_E.MoneyID = 7;
                        stock_BL.UpdateQuantityATM(stock_E);


                        cardE.CardNo = HttpContext.Current.Session["CardNo"].ToString();
                        accountBL.CheckWithdraw(accountE, cardE);
                        WriteLog();
                        Response.Redirect("WithdrawScreen03.aspx");
                    }

                    else
                    {
                        decimal Con;


                        int To500k = (int)TienRut / 500000;
                        if (To500k > T500k)
                        {
                            To500k = T500k;

                        }                      
                        Con = TienRut - To500k * 500000;
                        //------
                        int To200k = (int)Con / 200000;
                        if (To200k > T200k)
                        {
                            To200k = T200k;
                        }
                       
                        Con = Con - To200k * 200000;
                        //------
                        int To100k = (int)Con / 100000;
                        if (To100k > T100k)
                        {
                            To100k = T100k;

                        }
                       
                        Con = Con - To100k * 100000;

                        //-----
                        int To50k = (int)Con / 50000;
                        if (To50k > T50k)
                        {
                            To50k = T50k;

                        }
                        Con = Con - To50k * 50000;
                        //-----
                        
                        int To20k = (int)Con / 20000;
                        if (To20k > T10k)
                        {
                            To20k = T20k;

                        }
                         Con = Con - To20k * 20000;
                        //-----
                         int To10k = (int)Con / 10000;
                         if (T10k < To10k && Con > 10000)
                         {
                             lb_ThongBao.Text = "withdraw is not success, please trying again";
                         }
                         else {
                             

                             if (To10k > T10k)
                             {
                                 To10k = T10k;

                             }
                             UpdateQuantity(To500k, 1);
                             UpdateQuantity(To200k, 2);
                             UpdateQuantity(To100k, 3);
                             UpdateQuantity(To50k, 4);
                             UpdateQuantity(To20k, 6);
                             UpdateQuantity(To10k, 7);
                             cardE.CardNo = HttpContext.Current.Session["CardNo"].ToString();
                             accountBL.CheckWithdraw(accountE, cardE);
                             WriteLog();
                             Response.Redirect("WithdrawScreen03.aspx");
                             
                         }
                       
                        
                    }
                }
                else
                {
                    lb_ThongBao.Text = "* Amount is Invalid";
                }

            }
       
        }
        public void UpdateQuantity(int a, int b)
        {
            stock_E.Quantity = a;
            stock_E.MoneyID = b;
            stock_BL.UpdateQuantityATM(stock_E);
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("WithdrawScreen01.aspx");
        }

        protected void Button11_Click(object sender, EventArgs e)
        {
            RutTien();
        }

        protected void btn_Num1_Click(object sender, EventArgs e)
        {
            txt_Nhap.Text += 1;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            txt_Nhap.Text += 2;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            txt_Nhap.Text += 3;
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            txt_Nhap.Text += 4;
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            txt_Nhap.Text += 5;
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            txt_Nhap.Text += 6;
        }

        protected void Button8_Click(object sender, EventArgs e)
        {
            txt_Nhap.Text += 7;
        }

        protected void Button9_Click(object sender, EventArgs e)
        {
            txt_Nhap.Text += 8;
        }

        protected void Button10_Click(object sender, EventArgs e)
        {
            txt_Nhap.Text += 9;
        }

        protected void Button13_Click(object sender, EventArgs e)
        {
            txt_Nhap.Text += 0;
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            txt_Nhap.Text = "";
        }
    }
}